package com.k.kc.common.resp;

import java.io.Serializable;

/**
 * 响应类
 *
 * @param <T> the type of data
 * @author k
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private String code;
    private String message;
    private T data;

    public Result() {
    }
    public Result(T data){
        this.data = data;
        this.code = "200";
        this.message = "success";
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private static <T> Result<T> restResult(String code, String msg, T data) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }

    /**
     * 返回成功
     * @param data 成功数据
     * @param <T> type of data
     * @return Result
     */
    public static <T> Result<T> success(T data) {
        return restResult("200", "Success", data);
    }

    /**
     * 返回成功
     * @param <T> type of data
     * @return Result
     */
    public static <T> Result<T> success(){
        return restResult("200", "Success", null);
    }

    /**
     * 返回失败
     * @param <T> type of data
     * @return Result
     */
    public static <T> Result<T> failed() {
        return restResult("500", "Failed", null);
    }

    /**
     * 返回失败
     * @param msg 失败消息
     * @param <T> type of data
     * @return Result
     */
    public static <T> Result<T> failed(String msg) {
        return restResult("500", msg, null);
    }
}
